package com.roomaja.roomaja.mapper;

import com.roomaja.roomaja.entity.WenKuEntity;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface WenKuEntityMapper {

    @Insert("insert into wenku(doc_id,doc_type,doc_title,doc_author,doc_size, doc_desc, doc_label, entry_time) " +
            "values (#{docId}, #{docType}, #{docTitle}, #{docAuthor},#{docSize}, #{docDesc}, #{docLabel},now())  " +
            "on duplicate key update doc_title = #{docTitle},doc_author = #{docAuthor}, doc_size = #{docSize}, doc_desc =  #{docDesc}, doc_label = #{docLabel}")
    void insert(WenKuEntity wenKuEntity);

    @Select("select t.doc_id, t.doc_type, t.doc_title, t.doc_author, t.doc_size, t.downloads from wenku t where t.doc_title like concat('%',#{doc_title},'%')")
    @Results({
            @Result(id=true,column="doc_id",property="docId"),
            @Result(column="doc_type",property="docType"),
            @Result(column="doc_title",property="docTitle"),
            @Result(column="doc_author",property="docAuthor"),
            @Result(column="doc_size",property="docSize"),
            @Result(column="downloads",property="downloads"),
    })
    List<WenKuEntity> getWenkuList(@Param("doc_title") String doc_title);

    @Select("select count(t.doc_id) from wenku t where t.doc_title like concat('%',#{doc_title},'%')")
    int countWenKuList(String doc_title);

    @Select({"select count(t.doc_id) from wenku t where t.doc_id = #{docId}"})
    int isExistByDocId(String var1);

    @Select({"select t.downloads from wenku t where t.doc_id = #{docId}"})
    int isEverDownload(String docId);

    @Select("select t.doc_id, t.doc_type, t.doc_title, t.doc_author, t.doc_size, t.downloads, t.doc_desc, t.doc_label from wenku t where t.doc_id = #{doc_id}")
    @Results({
            @Result(id=true,column="doc_id",property="docId"),
            @Result(column="doc_type",property="docType"),
            @Result(column="doc_title",property="docTitle"),
            @Result(column="doc_author",property="docAuthor"),
            @Result(column="doc_size",property="docSize"),
            @Result(column="downloads",property="downloads"),
            @Result(column="doc_desc",property="docDesc"),
            @Result(column="doc_label",property="docLabel"),
    })
    List<WenKuEntity> getDetailByDocId(String doc_id);
}
